package click

class HourRateController {

    static allowedMethods = [save: "POST", update: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        [hourRateInstanceList: HourRate.list(params), hourRateInstanceTotal: HourRate.count()]
    }

    def create = {
        def hourRateInstance = new HourRate()
        hourRateInstance.properties = params
        return [hourRateInstance: hourRateInstance]
    }

    def save = {
        def hourRateInstance = new HourRate(params)
        if (hourRateInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'hourRate.label', default: 'HourRate'), hourRateInstance])}"
            redirect(action: "list")
        }
        else {
            render(view: "create", model: [hourRateInstance: hourRateInstance])
        }
    }

    def show = {
        def hourRateInstance = HourRate.get(params.id)
        if (!hourRateInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'hourRate.label', default: 'HourRate'), params.id])}"
            redirect(action: "list")
        }
        else {
            [hourRateInstance: hourRateInstance]
        }
    }

    def edit = {
        def hourRateInstance = HourRate.get(params.id)
        if (!hourRateInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'hourRate.label', default: 'HourRate'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [hourRateInstance: hourRateInstance]
        }
    }

    def update = {
        def hourRateInstance = HourRate.get(params.id)
        if (hourRateInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (hourRateInstance.version > version) {
                    
                    hourRateInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'hourRate.label', default: 'HourRate')] as Object[], "Another user has updated this HourRate while you were editing")
                    render(view: "edit", model: [hourRateInstance: hourRateInstance])
                    return
                }
            }
            hourRateInstance.properties = params
            if (!hourRateInstance.hasErrors() && hourRateInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'hourRate.label', default: 'HourRate'), hourRateInstance])}"
                redirect(action: "list")
            }
            else {
                render(view: "edit", model: [hourRateInstance: hourRateInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'hourRate.label', default: 'HourRate'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def hourRateInstance = HourRate.get(params.id)
        if (hourRateInstance) {
            try {
                hourRateInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'hourRate.label', default: 'HourRate'), hourRateInstance])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'hourRate.label', default: 'HourRate'), hourRateInstance])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'hourRate.label', default: 'HourRate'), params.id])}"
            redirect(action: "list")
        }
    }


    def createAll = { 
        def website = Website.get(params.websiteid)
        if (!website) {
           flash.message = "没有找到 websiteid，你这个页面是咋整的？？"
           
        }
        return [website: website]
    }

    def saveAll = {
        def website = Website.get(params.websiteid)
        if (!website) {
           flash.message = "没有找到 websiteid，你这个页面是咋整的？？"
            render(view: "createAll", model: ['websiteid' : params.websiteid])
        }

        //def webActionList = WebAction.findAllByTask(task) 
 
        if (website.rate)  {
                website.rate.toList().each {
                        website.rate.remove(it)
                        it.delete()
                } 
        } 
//        website.rate.each{
//           // it.task.actions.remove(it) 
//            it.task = null
//            it.delete()
//        } 

        //def webactions = params.content.split("\r\n")
        def hour = 0
        if (params.rate) {
            if (params.rate in String[]) {
                params.rate.each{
                    website.rate.add(new HourRate(hour: hour, rate : it , website : website));
                    hour++
                } 
            }
        }
       
         redirect(action: "list",controller: 'website')
 
    }

}
